// needed wx classes
#include <wx/protocol/http.h>
#include <wx/sstream.h>

#include "CodePasteUtils.h"

const wxString wxURLEncode(const wxString& original)
{
   wxString ret = original;
   ret.Replace(_T("%"),_T("%25"),true);
   ret.Replace(_T("?"),_T("%3F"),true);
   ret.Replace(_T("&"),_T("%26"),true);
   ret.Replace(_T("#"),_T("%23"),true);
   ret.Replace(_T("<"),_T("%3C"),true);
   ret.Replace(_T(">"),_T("%3E"),true);
   ret.Replace(_T("\t"),_T("%09"),true);
   ret.Replace(_T(" "),_T("%20"),true);
   ret.Replace(_T("\n"),_T("%0A"),true);
   ret.Replace(_T("\""),_T("%22"),true);
   return ret;
}

const wxString wxHTTP_POST(const wxString& server, const wxString& page, const wxString& parameters)
{
   wxString reply = wxEmptyString;
   wxHTTP http; http.SetHeader(_T("Content-type"), _T("application/x-www-form-urlencoded"));
   http.SetPostBuffer(parameters); http.Connect(server);
   wxInputStream *http_stream = http.GetInputStream(_T("/")+page);
   if (http.GetError() == wxPROTO_NOERR)
   {
      wxStringOutputStream out_stream(&reply);
      http_stream->Read(out_stream);
   }
   wxDELETE(http_stream); http.Close();
   return reply;
}

